package com.hx.util.httpclient;

import java.io.IOException;
import java.io.Serializable;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;


public class HttpSendObject {
	 /**
	     * @param url
	     * @param queryString 类似a=b&c=d 形式的参数
	     * @param inputObj   发送到服务器的对象。
	     * @return 服务器返回到客户端的对象。
	     * @throws IOException
	     */
	    public Object getObjFromServer(String url, String queryString, Object inputObj) throws IOException
	    {
	        HttpClient client = new HttpClient();
	        PostMethod post = new PostMethod(url);
	        post.setQueryString(queryString);
	        post.setRequestHeader("Content-Type", "application/octet-stream");
	        java.io.ByteArrayOutputStream bOut = new java.io.ByteArrayOutputStream(1024);
	        java.io.ByteArrayInputStream bInput = null;
	        java.io.ObjectOutputStream out = null;
	        Serializable returnObj = null;
	        try
	        {
	            out = new java.io.ObjectOutputStream(bOut);
	            out.writeObject(inputObj);
	            out.flush();
	            out.close();
	            out = null;
	            bInput = new java.io.ByteArrayInputStream(bOut.toByteArray());
	            RequestEntity re = new InputStreamRequestEntity(bInput);
	            post.setRequestEntity(re);
	            client.executeMethod(post);
	            java.io.InputStream in = post.getResponseBodyAsStream();
	            java.io.ObjectInputStream oInput = new java.io.ObjectInputStream(in);
	            returnObj = (Serializable) oInput.readObject();
	            oInput.close();
	            oInput = null;
	        }
	        catch (IOException e)
	        {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	        catch (ClassNotFoundException e)
	        {
	            // TODO Auto-generated catch block
	            e.printStackTrace();
	        }
	        finally
	        {
	            if (out != null)
	            {
	                out.close();
	                out = null;
	            }
	            if (bInput != null)
	            {
	                bInput.close();
	                bInput = null;
	            }
	            //释放连接
	            post.releaseConnection();
	        }
	        return returnObj;
	    }
	    
	    public static void main(String[] args) {
	    	HttpSendObject sendObject=new HttpSendObject();
	    	try {
//	    		发送对象
//	    		User user=new User();
//	    		user.setUserName("许进");
	    		
//	    		发送map
//	    		Map map=new HashMap();
//	    		map.put("xujin", "许进");
	    	
	    		//泛型也支持
	    		
//	    		Map<String,User> map=new HashMap<String,User>();
//	    		User user=new User();
//	    		user.setUserName("许进");
//	    		map.put("xujin",user );
	    		
//	    		List list=new ArrayList();
//	    		list.add("许进");
	    		Boolean aa=(Boolean)sendObject.getObjFromServer("http://localhost:8088/LIANPAY_NETGATE/netgate/line.action", "qq=582522680", "");
	    		System.out.println("==================="+aa);
				System.out.println();;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	    
	   
}
